﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web.Mvc;
using HelloMinasan.Business;
using HelloMinasan.Domain;
using HelloMinasan.Resources;
using HelloMinasan.UserDefinedType;

namespace HelloMinasan.Web.ViewModels
{
    public class MemberSettingsModel
    {
        public MemberSettingsModel()
        {
            this.NotificationSettingModel = new MemberNotificationSettingsModel();
            this.PrivacySettingModel = new MemberPrivacySettingsModel();
            //this.WithingAccountSettingsModel = new WithingAccountSettingsModel();
            ListLanguage = new List<Language>();
        }

        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "MemberRegister_YourDisplayNameEmpty")]
        //[RegularExpression(@"{5,50}", ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "DisplaynameInvalidObject")]
        [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resource),
            ErrorMessageResourceName = "MemberSetting_PleaseInputAtLeast", MinimumLength = 5)]
        public string DisplayName { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "YourEmailEmpty")]
        [Remote("IsValidEmail", "MemberSettingValidate")]
        //[RegularExpression(@"^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$", ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InvalidObject")]
        [RegularExpression(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$", ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "EmailInvalid")]
        [StringLength(100, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InValidMaxSize")]
        public string Email { get; set; }

        [RegularExpression(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$", ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "EmailInvalid")]
        [StringLength(100, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InValidMaxSize")]
        public string EmergencyEmail { get; set; }

        [StringLength(20, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InValidMaxSize")]
        //[Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "MemberSettings_CellPhoneEmpty")]
        public string CellPhone { get; set; }

        public IList<Language> ListLanguage { get; set; }
        [DisplayName("Language")]
        public int Language { get; set; } //language for layout
        //public int NativeLanguage { get; set; } //mother language of user
        public int ForeignLanguage1 { get; set; } //first foreign language user can speak or be learning
        public int ForeignLanguage2 { get; set; } //second foreign language user can speak or be learning
        public int ForeignLanguage3 { get; set; } //third foreign language user can speak or be learning

        public EnumDictionary DictionaryDefault { get; set; }
        public IList<DictionarySupport> ListDictionary { get; set; }

        public virtual MemberPrivacySettingsModel PrivacySettingModel { get; set; }
        public virtual MemberNotificationSettingsModel NotificationSettingModel { get; set; }
        //public virtual WithingAccountSettingsModel WithingAccountSettingsModel { get; set; }

        //public virtual ParameterModel ParameterModel { get; set; }
    }

    public class MemberSettingValidateController : Controller
    {
        private IUserInvitationService userInvitationService;

        public MemberSettingValidateController(IUserInvitationService userInvitationService)
        {
            this.userInvitationService = userInvitationService;
        }

        public JsonResult IsValidEmail(string email)
        {
            User user = userInvitationService.FindAll<User>(x => x.Email == email.ToLower()).FirstOrDefault();
            bool isUserInvitationEmailExist = userInvitationService.FindAll<UserInvitation>(x => x.Email == email.ToLower()).Any();
            if ((user != null && user.Id != int.Parse(System.Web.HttpContext.Current.User.Identity.Name))
                || isUserInvitationEmailExist == true)
            {
                return Json(string.Format(Resource.DuplicateField, Resource.HomeLogin_Email), JsonRequestBehavior.AllowGet);
            }
            return Json(true, JsonRequestBehavior.AllowGet);
        }
    }
}